/**
 * 
 */
package com.chh.mysso.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chh.mysso.common.enums.ErrorCodeEnum;
import com.chh.mysso.common.utils.MD5HashUtil;
import com.chh.mysso.common.utils.PwdUtil;
import com.chh.mysso.core.cache.LoginUserCache;
import com.chh.mysso.core.dao.AuthUserDAO;
import com.chh.mysso.core.entity.AuthUser;
import com.chh.mysso.core.service.AuthUserService;
import com.chh.mysso.domain.exception.SSOException;
import com.chh.mysso.domain.model.dto.AuthUserDTO;
import com.chh.mysso.domain.pojo.command.user.AddAuthUserRequest;
import com.chh.mysso.domain.pojo.command.user.LoginAuthUserRequest;
import com.chh.mysso.domain.pojo.command.user.ModifyAuthUserRequest;
import com.chh.mysso.domain.pojo.qo.AuthUserQo;

/**
 * @author chh
 *
 */
@Service
@Transactional
public class AuthUserServiceImpl implements AuthUserService {
	
	@Autowired
	private AuthUserDAO authUserMapper;
	
	@Autowired
	private LoginUserCache loginUserCache;
	
	@Override
	public AuthUserDTO queryByUserName(String userName) {
		AuthUserDTO dto = new AuthUserDTO();
		BeanUtils.copyProperties(authUserMapper.queryUnique(userName), dto);
		return dto;
	}

	/**
	 * @see com.chh.mysso.core.service.AuthUserService#query(com.chh.mysso.domain.pojo.qo.AuthUserQo)
	 */
	@Override
	public List<AuthUserDTO> query(AuthUserQo qo) {
		
		List<AuthUserDTO> resList = new ArrayList<>();
		AuthUserDTO auth = new AuthUserDTO();
		auth.setEmail("chh@163.com");
		resList.add(auth);
		
		return resList;
	}

	/**
	 * @see com.chh.mysso.core.service.AuthUserService#login(com.chh.mysso.domain.pojo.command.user.LoginAuthUserRequest)
	 */
	@Override
	public AuthUserDTO login(LoginAuthUserRequest request) {
		AuthUser authUser = authUserMapper.queryUnique(request.getLoginName());
		if (authUser == null) {
			throw new SSOException(ErrorCodeEnum.USER_NOT_EXIST, "用户不存在");
		} 
		if (!StringUtils.equals(authUser.getPassword(), PwdUtil.getPwd(request.getPassword()))) {
			throw new SSOException(ErrorCodeEnum.USER_PWD_ERROR, "密码不匹配");
		}
		AuthUserDTO dto = new AuthUserDTO();
		BeanUtils.copyProperties(authUser, dto);
		loginUserCache.putLoginUser(MD5HashUtil.toMD5(request.getLoginToken()), dto);
		return dto;
	}
	
	/**
	 * @see com.chh.mysso.core.service.AuthUserService#add(com.chh.mysso.domain.pojo.command.user.AddAuthUserRequest)
	 */
	@Override
	public void add(AddAuthUserRequest request) {
	}

	/**
	 * @see com.chh.mysso.core.service.AuthUserService#modify(com.chh.mysso.domain.pojo.command.user.ModifyAuthUserRequest)
	 */
	@Override
	public void modify(ModifyAuthUserRequest request) {

	}

}
